package com.tb.attendancesystem;

/**
 * 登录活动
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = LoginActivity.class.getSimpleName();
    private int status;			//记录服务器返回状态
    private final static int REQUSET_ERROR = -1;
    private final static int TEACHER = 1;
    private final static int STUDENT = 2;
    private final static int PSW_ERROR = 3;
    private final static int NOT_EXIST = 4;

    private EditText etId;
    private EditText etPsw;
    private Button btLogin;

    //加载资源
    private void loadRes() {
        etId = (EditText) findViewById(R.id.et_login_id);
        etPsw = (EditText) findViewById(R.id.et_password);
        btLogin = (Button) findViewById(R.id.bt_login);

    }

    //监听注册
    private void register() {
        btLogin.setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loadRes();
        register();
    }


    @Override
    public void onClick(View v) {
        if (v.equals(btLogin)) {
            if (etId.getText().toString().isEmpty()) {
                Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show();
            } else if (etPsw.getText().toString().isEmpty()) {
                Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
            } else {
                login(etId.getText().toString(), etPsw.getText().toString());
            }
        }
    }

    //提供账号密码进行登录
    private void login(String userId, String password) {
        OkHttpUtils.get()
                .url(getResources().getString(R.string.host_url) + "/AttendanceSystem/LoginCheck")
                .addParams("id", userId)
                .addParams("psw", password)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        dealOfResponse(response);
                    }
                });
    }

    //对登录返回的结果进行处理
    private void dealOfResponse(String response) {
        Log.d(TAG, "登录返回的response是：" + response);
        status = Integer.parseInt(response);
        Log.d(TAG, "登录返回的status是：" + status);
        Intent intent = new Intent();
        intent.putExtra("userId", etId.getText().toString());
        switch (status) {
            case STUDENT:
                intent.setClass(this, com.tb.attendancesystem.student.MainActivity.class);
                startActivity(intent);
                break;
            case TEACHER:
                intent.setClass(this, com.tb.attendancesystem.teacher.MainActivity.class);
                startActivity(intent);
                break;
            case NOT_EXIST:
                Toast.makeText(this, "账号不存在！", Toast.LENGTH_SHORT).show();
                break;
            case PSW_ERROR:
                Toast.makeText(this, "密码错误！", Toast.LENGTH_SHORT).show();
                break;
            case REQUSET_ERROR:
                Toast.makeText(this, "服务器请求错误！", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(this, "未知错误！", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
